package lzupdater;

import java.io.File;
import java.io.FileNotFoundException;

import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import lzupdater.com.CommunicationHub;
import lzupdater.gui.MainGui;
import lzupdater.locale.Dictionary;
import lzupdater.locale.Locale;
import lzupdater.locale.LocalizedString;
import lzupdater.log.ConsoleLogListener;
import lzupdater.log.DefaultLogger;
import lzupdater.log.FileLogListener;
import lzupdater.log.Logger;
import lzupdater.log.LoggerLevel;
import lzupdater.net.LZUpdater;
import lzupdater.utils.LZULogger;
import lzupdater.utils.LZUPreferences;

//LZRosterUpdater, a java client to update LZRoster
//Copyright (C) 2008 Fraskor from Krasus (e-mail: fraskor@gmail.com)

//This file is part of LZRosterUpdater.

//LZRosterUpdater is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Main class that initialize the communication between the components and
 * launch the GUI
 * 
 * @author Fraskor
 */
public class LZRosterUpdater {

	public static final String VERSION = "2.0";
	public static final String AuthorName = "Fraskor";
	public static final String AuthorServer = "Krasus (EU)";

	private static final String PREFERENCE_FILE = "lzupdater_preferences.txt";
	private static final String LOG_FILE = "lzupdater_log.txt";

	private static final LocalizedString initialize = new LocalizedString("Initialize LZRosterUpdater");
	private static final LocalizedString initializationDone = new LocalizedString("Initialization done");
	private static final LocalizedString updaterReady = new LocalizedString("LZRosterUpdater ready ...");
	
	/**
	 * Main method that launch the application
	 */
	public static void main(String[] args) {

		
		if (args.length > 0
				&& (args[0].equals("--version") || args[0].equals("-v"))) {
			System.out.println(VERSION);
		} else {			
			SwingUtilities.invokeLater(getGuiRunnable());
		}
	}


	private static Runnable getGuiRunnable() {
		return new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager
							.getSystemLookAndFeelClassName());
				} catch (Exception e) {
					//
				}
				Logger rootLogger = new DefaultLogger();

				Dictionary.setLogger(rootLogger);
				Dictionary.setCurrentLanguage(Locale.getUserLocale());

				CommunicationHub hub = new CommunicationHub();

				Logger logger = new LZULogger(hub, rootLogger);
				initializeLoggers(logger);

				new LZUpdater(hub);
				LZUPreferences pref = new LZUPreferences(hub);
				MainGui gui = new MainGui(hub);

				hub.initialize();

				logger.info(initialize);

				gui.launch();

				pref.setPreferenceFile(PREFERENCE_FILE);
				pref.load();

				Dictionary.setCurrentLanguage(new Locale(pref.getLang()));

				logger.info(initializationDone);
				logger.info(updaterReady);
			}

			private void initializeLoggers(Logger logger) {
				// Add a file logger if possible
				try {
					FileLogListener fileLog = new FileLogListener(LOG_FILE);
					logger.addLoggerListener(fileLog, false);
					logger.format(LoggerLevel.INFO, "Log file : '%s'",
							fileLog.getLogPath());
				} catch (FileNotFoundException e) {
					logger.format(LoggerLevel.WARNING,
							"Could not create log file : '%s'", new File(
									LOG_FILE).getAbsoluteFile());
					// add the console if the file could not be created
					logger.addLoggerListener(new ConsoleLogListener(),
							false);
				}
			}

		};
	}
}
